package com.imooc.mall;

import com.imooc.mall.consts.MallConst;
import com.imooc.mall.exception.UserLoginException;
import com.imooc.mall.pojo.User;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;


/**
 * @author mood
 */
@Slf4j
public class UserLoginInterceptor implements HandlerInterceptor {

  /**
   * @Author: MoodAir
   * @Description: 登陆校验
   * @Date: 2020/9/11 下午5:52
   * @param: request
   * @param: response
   * @param: handler
   * @return: boolean
   * @Version: 1.0
   */
  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
    log.info("preHandle...");
    User user = (User) request.getSession().getAttribute(MallConst.CURRENT_USER);
    if (user == null) {
      log.info("user=null");
      throw new UserLoginException();
    }
    return true;
  }
}